package com.asset.salesforce.controller;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.asset.salesforce.converter.LeadConverter;
import com.asset.salesforce.domain.Account;
import com.asset.salesforce.domain.Contact;
import com.asset.salesforce.domain.Employee;
import com.asset.salesforce.domain.Lead;
import com.asset.salesforce.domain.StockSku;
import com.asset.salesforce.form.BasicView;
import com.asset.salesforce.form.LeadForm;
import com.asset.salesforce.service.CalendarService;
import com.asset.salesforce.service.CheckingUtilsService;
import com.asset.salesforce.service.JoinRequestService;
import com.asset.salesforce.service.SearchService;

@Controller
public class IndexingController {
	
	@Autowired
	SearchService searchService ;
	
	@Autowired
	private CheckingUtilsService checkingUtilsService;
	
	@Autowired
	private JoinRequestService joinRequestService;
	
	@Autowired
	private CalendarService calendarService;
	@RequestMapping(value="/indexing" ,method = RequestMethod.GET)
	public void index() {
		searchService.index();
	}
	@RequestMapping(value="/search" ,method = RequestMethod.POST)
	public String search(String name,HttpSession session,Model model,
			@RequestParam(value="query",required=false)String query) {
		if(null ==session.getAttribute("userDetails") ){
			return "redirect:/s/login.html";
		}
		//searchService.index();
		Set<Lead> leadSet =searchService.searchLead(query);
		Set<Lead> leads =new HashSet<Lead>();
		//System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"+leads.size());
		Employee owner =(Employee)session.getAttribute("userDetails");
		
		for (Lead lead : leadSet) {
			if(lead.getLeadOwner().getCompany().getId().equals(owner.getCompany().getId()) && !lead.getIsDeleted() ){
				leads.add(lead);
				
			}
		}
		for (Lead lead : leads) {
			if(lead.getIsDeleted().equals(false)){
				lead.setIsFollowed(checkingUtilsService.checkIfCurrentFollowThatLead(owner, lead));
			}
		}
			model.addAttribute("leads", leads);
			joinRequestService.getJoinRquestNo(session, model);
			calendarService.getAppointmentEventsNo(session, model);
			calendarService.getCallEventssNo(session, model);
			model.addAttribute("userDetails",owner);
			model.addAttribute("query",query);
			model.addAttribute("index", 0);
			model.addAttribute("pages", 0);
		return "leads";
		
	}
	@RequestMapping(value="/searchAccount" ,method = RequestMethod.POST)
	public  String searchAccount(String name,HttpSession session,Model model,
			@RequestParam(value="query",required=false)String query) {
		if(null ==session.getAttribute("userDetails") ){
			return "redirect:/s/login.html";
		}
		//searchService.index();
		Set<Account> accountSet = searchService.searchAccount(query);
		Set<Account> accounts = new HashSet<Account>();
		//System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"+accounts.size());
			if(null ==session.getAttribute("userDetails") ){
				return "redirect:/s/login.html";
			}
			Employee userDetails = (Employee)session.getAttribute("userDetails");
			if(accountSet != null){
				for(Account account : accounts){
					if(account.getAccountCreator().getCompany().getId().equals(userDetails.getCompany().getId())&&!account.getIsDeleted()){
						accounts.add(account);	
					}
					
				}
				
			}else{
				accountSet = new HashSet<Account>(); 
			}
			if(accounts!=null && accounts.size()>0){
				for (Account account : accounts) {
					if(checkingUtilsService.checkIfCurrentFollowThatAccount(userDetails, account)){
						account.setIsFollowed(true);
					}else{
						account.setIsFollowed(false);
					}
				}
			}
			
			model.addAttribute("accounts", accounts);
			joinRequestService.getJoinRquestNo(session, model);
			calendarService.getAppointmentEventsNo(session, model);
			calendarService.getCallEventssNo(session, model);
			model.addAttribute("userDetails",userDetails);
			model.addAttribute("query",query);
			model.addAttribute("index", 0);
			model.addAttribute("pages", 0);
			return "accounts";
		
	}
	
	@RequestMapping(value="/searchContact" ,method = RequestMethod.POST)
	public  String searchContact(String name,HttpSession session,Model model,
			@RequestParam(value="query",required=false)String query) {
		if(null ==session.getAttribute("userDetails") ){
			return "redirect:/s/login.html";
		}
		//searchService.index();
		List<Contact> contacts = new ArrayList<Contact>();
		List<Contact> contactSet = searchService.searchContact(query);
		//System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"+contacts.size());
			Employee userDetails = (Employee)session.getAttribute("userDetails");
			model.addAttribute("userDetails",userDetails);
			if(contactSet != null){
				for(Contact contact:contactSet){
					if(contact.getCreator().getCompany().getId().equals(userDetails.getCompany().getId()) && !contact.getIsDeleted() ){
						contacts.add(contact);
					}
				}
			}
			if(contacts!=null && contacts.size()>0){
			for (Contact contact : contacts) {
				if(checkingUtilsService.checkIfCurrentFollowThatContact(userDetails, contact)){
					contact.setIsFollowed(true);
				}else{
					contact.setIsFollowed(false);
				}
			}
			}
			model.addAttribute("contacts", contacts);
			joinRequestService.getJoinRquestNo(session, model);
			calendarService.getAppointmentEventsNo(session, model);
			calendarService.getCallEventssNo(session, model);
			model.addAttribute("userDetails",userDetails);
			model.addAttribute("query",query);
			model.addAttribute("index", 0);
			model.addAttribute("pages", 0);
			return "contacts";
		
	}
	@RequestMapping(value="/searchBoth" ,method = RequestMethod.POST)
	public  String searchAccountAndLeada(String name,HttpSession session,Model model,
			@RequestParam(value="query",required=false)String query) {
		
		if(null ==session.getAttribute("userDetails") ){
			return "redirect:/s/login.html";
		}
		Employee userDetails = (Employee)session.getAttribute("userDetails");
		Set<Lead>leads = new HashSet<Lead>();
		 leads =searchService.searchLead(query);
		 Set<Account>  accounts = new HashSet<Account>();
		 accounts = searchService.searchAccount(query);
		Set<BasicView>basicViews = new HashSet<BasicView>();
		if( leads != null){
			for(Lead lead : leads){
				if(lead.getLeadOwner().getCompany().getId().equals(userDetails.getCompany().getId()) && !lead.getIsDeleted()){
					BasicView basicView = new BasicView(null, lead);
					basicViews.add(basicView);
				}
			}
			
		}
		if( accounts != null){
			for(Account account : accounts){
				if(account.getAccountCreator().getCompany().getId().equals(userDetails.getCompany().getId()) && !account.getIsDeleted()){
					BasicView basicView = new BasicView(account, null);
					basicViews.add(basicView);
				}
			}
			
		}
		if(basicViews != null && basicViews.size() >0){
			
			model.addAttribute("basicViews", basicViews);
		}
		return "search_results";
	}
	@RequestMapping(value="/searchSku" ,method = RequestMethod.POST)
	public  String searchSku(String name,HttpSession session,Model model,
			@RequestParam(value="query",required=false)String query) {
		Set<StockSku> skus = new HashSet<StockSku>();
		skus = searchService.searchSku(query);
		if(skus.size()>0){
			model.addAttribute("basicViews", skus);
		}
		System.out.println("Skusss>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>"+skus.size());
		return "search-results-sku";
	}

}
